package cn.com.czpb.client.module.news.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.StyleRes;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import cn.com.czpb.client.R;
import cn.com.czpb.client.utils.PageCtrl;
import cn.com.czpb.client.utils.SPUtil;
import cn.com.czpb.client.utils.TUtils;

/**
 * Created by Administrator on 2017/8/17.
 */

public class ReplyDialog extends Dialog implements View.OnClickListener {
    EditText et_reply;
    TextView tv_send;
    private Context mContext;
    private SPUtil spu;
    private String nickName;
    public interface InputListener {
        void inputstr(String string);
    }
    private InputListener listener;
    public ReplyDialog(Context context) {
        super(context);
        this.mContext=context;
        init();
    }

    public ReplyDialog(@NonNull Context context, @StyleRes int themeResId,InputListener h) {
        super(context, themeResId);
        this.mContext=context;
        this.listener=h;
        init();
    }public ReplyDialog(@NonNull Context context, @StyleRes int themeResId,String nickname,InputListener h) {
        super(context, themeResId);
        this.mContext=context;
        this.listener=h;
        this.nickName=nickname;
        init();
    }

    private void init() {
        spu=SPUtil.getInstance();
        WindowManager wm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
        int width = wm.getDefaultDisplay().getWidth();
        View view = LayoutInflater.from(mContext).inflate(R.layout.et_reply_laout, null);
        setContentView(view);
        et_reply = view.findViewById(R.id.et_reply);
        tv_send = view.findViewById(R.id.tv_send);
        tv_send.setOnClickListener(this);
        et_reply.setHint("回复："+nickName);
        Window dialogWindow = getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        dialogWindow.setGravity(Gravity.END | Gravity.BOTTOM);
        lp.width = width;
        dialogWindow.setAttributes(lp);
    }

    @Override
    public void onClick(View view) {
        String msg = et_reply.getText().toString().trim();
        if (spu.getUser() == null) {
            PageCtrl.start2LoginActivity((Activity) mContext);
        } else {

            if (msg == null) {
                TUtils.toast("请输入内容");
            } else {
                if (listener != null) {
                    listener.inputstr(msg);
                }
                et_reply.setText("");
                this.dismiss();
            }
        }
    }


}
